/*
 * ResourceManager.java
 * 
 *  author: Daniel Rudigier
 *    date: 01.12.2008
 * 
 * 
 */
package at.syncme.framework.midp;

import java.io.IOException;

import javax.microedition.lcdui.Image;

import de.enough.polish.util.HashMap;
import de.enough.polish.util.Properties;


/**
 * provides functionality to load different resources into the game.
 * <p>
 * for handling different languages, a mapping will be used. usually, only one
 * language is needed at the same time, therefore only one language has to be
 * stored simultaneously.
 * </p>
 * 
 * @author Daniel Rudigier
 */
public class ResourceManager {    
    protected static ResourceManager instance;
    
    protected Properties language;
    
    protected Properties configuration;
    
    protected HashMap images = new HashMap();

    /**
     * protected constructor
     */
    protected ResourceManager() {
        // singleton
    }

    /**
     * returns the translated text of the given token according to the current
     * language
     * 
     * @param token
     * @return
     */
    public String getTextFor(String token) {
        if (this.language.containsKey(token)) {
            return this.language.getProperty(token);
        } else {
            return "#" + token;
        }
    }
    
    /**
     * returns the configuration value of the given token
     * 
     * @param token
     * @return
     */
    public String getCfgFor(String token) {
        if (this.configuration.containsKey(token)) {
            return this.configuration.getProperty(token);
        } else {
            return "#" + token;
        }
    }
    
    /**
     * parse the configuration using a properties file
     * 
     * @param propertiesFile
     */
    public void loadConfiguration(String propertiesFile) {
        try  {
            this.configuration = new Properties(propertiesFile);
        } catch (IOException e) {
            System.out.println("configuration error: ");
            e.printStackTrace();
        }
    }

    /**
     * parse the language using a properties file
     * 
     * @param propertiesFile
     */
    public void loadLanguage(String propertiesFile) {
        try  {
            this.language = new Properties(propertiesFile);
        } catch (IOException e) {
            System.out.println("language error: ");
            e.printStackTrace();
        }
    }
    
    /**
     * create an image
     * 
     * @param source 
     * @return
     */
    public Image loadImage(String source) {
        Image ret = (Image) this.images.get(source);
        
        try {
            if (ret == null) {
                ret = Image.createImage(source);
                this.images.put(source, ret);
            } 
        } catch (IOException e) {
            throw new RuntimeException("could not load: " + source);
        }
        
        return ret;
    }

    /**
     * transform an image 
     * 
     * @param location 
     * @param x
     * @param y 
     * @param w 
     * @param h 
     * @param t 
     * 
     * @return
     */
    public Image loadImage(String location, int x, int y, int w,
            int h, int t) {
        return Image.createImage(loadImage(location), x, y, w, h, t);
    }

    /**
     * return the instance
     * 
     * @return instance
     */
    public static Object getInstance() {
        if (instance == null) {
            instance = new ResourceManager();
        }
        
        return instance;
    }

}
